<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\news;
use app\api\controller\HomeCommon;
use app\api\model\NewsCategory;
use app\lib\exception\NotFoundException;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use think\Controller;
use think\Request;

class News extends Controller
{
    /**
     * 获取新闻列表
     * @param Request $request
     */
    public function getNewsList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        if(isset($post['category_id'])&&$post['category_id']!=0){
            $where = addWhere($post,['category_id'=>''],$where,1);
        }
        $where['page'] = $post['page'];
        $dataList = (new \app\api\model\News())->wechatListTotal($where,"id,title,category_id,cover_img,create_time","sort desc",$post['page_number']);
        return writeJson(200,$dataList);
    }
    /**
     * 查询资讯的分类
     */
    public function  getNewsCategory(){
       $category = (new NewsCategory())->selectList([],"id,name,sort","sort desc");
       $category = $category->toArray();
       array_unshift($category,['id'=>'','name'=>'全部','sort'=>100]);
        return writeJson(200,['category'=>$category]);
    }

    /**
     * 获取富文本内容
     * @param Request $request
     */
    public function getContentDetail(Request $request){
        $post = $request->post();
        if($post['type']==1){
            $detail = (new \app\api\model\News())->where([['id','eq',$post['id']]])->value('content');
        }else{
            $detail = (new \app\api\model\Product())->where([['id','eq',$post['id']]])->value('description');
        }
        $detail = html_entity_decode($detail);
        $detail = html_entity_decode($detail);
        return writeJson(200,['detail'=>$detail]);
    }

    /**
     * ceshi
     */
    public function getCeshi(){
        //dump(7878);die;
        $post = input();
        //return writeJson(200,['detail'=>$post]);
        if($post['type']==1){
            $detail = (new \app\api\model\News())->where([['id','eq',$post['id']]])->value('content');
        }else{
            $detail = (new \app\api\model\Product())->where([['id','eq',$post['id']]])->value('description');
        }
        $detail = html_entity_decode($detail);
        $detail = html_entity_decode($detail);
        //dump($detail);
        return writeJson(200,['detail'=>$detail]);
//        $this->assign('detail',$detail);
//        return $this->fetch();
    }

    /**
     * 测试aaa
     */
    public function ceshiaaa(){
        $this->assign('detail','aaa');

        //dump(111);die;
        return $this->fetch();
    }
    /**
     * 获取资讯详情
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     */
    public function getNewsDetail(Request $request){
        (new IdMustPositive())->goCheck();
        $post = $request->post();
        $detail = (new \app\api\model\News())->findOne([['id','eq',$post['id']]],'id,title,cover_img,content,create_time');
        if(!$detail){
            throw new NotFoundException(['msg'=>'资讯不存在']);
        }
        $detail['cover_img'] = imgUrlToAbsolute($detail['cover_img']);
        $detail['content'] = html_entity_decode($detail['content']);
        $detail['content'] = html_entity_decode($detail['content']);
        //$detail['content_url'] = 'http://api.globaltc.cn/api/news/getCeshi/id='.$post['id'].'/type=1';
        $detail['content_url'] = 'http://wx.youywt.com/index/index/ceshilink/id/'.$post['id'].'/type/1';
        //$detail['content'] = str_replace("<img ", "<img style=\"width:100%;height:auto;\"", $detail['content']);

        return writeJson(200,['detail'=>$detail]);
    }
}